package com.dhcc.bpm.modules.log.controller;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dhcc.bpm.common.annotation.Log;
import com.dhcc.bpm.common.config.ElasticsearchConfig;
import com.dhcc.bpm.common.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.tool.api.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Title
 * @ClassName EsTracNoLogController
 * @Desription
 * @Author zhangzexu
 * @Date 2020/12/7 下午2:09
 * @Version V1.0
 */
@RestController
@AllArgsConstructor
@NoArgsConstructor
@RequestMapping("bpm-gateway/gatewaylog")
@Api(value = "流水日志访问记录表", tags = "流水日志访问记录表")
public class EsTraceNoLogController {
    @Autowired
    ElasticsearchConfig elasticsearchConfig;


    @PostMapping("/selectTraceNoLog")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "查询交易日志", notes = "传入流水号")
    @Log(title = "查询交易日志", businessType = BusinessType.SELECT)
    @ApiLog("查询交易日志")
    public R selectTraceNoLog(@RequestBody JSONArray traceNos) {

        JSONObject root = new JSONObject();
        JSONObject query = new JSONObject();
        JSONObject bool = new JSONObject();
        JSONArray must = new JSONArray();

        JSONObject terms = new JSONObject();
        JSONObject traceNo = new JSONObject();
        traceNo.put("traceNo.keyword",traceNos);
        terms.put("terms",traceNo);
        must.add(terms);
        bool.put("must",must);
        query.put("bool",bool);
        root.put("query",query);
        HttpResponse response = HttpRequest.post(elasticsearchConfig.getHost())
                .header("Content-Type", "application/json")
                .header("cache-control", "no-cache")
                .header("Authorization", "Basic "+elasticsearchConfig.getBasic())
                .timeout(20000)
                .body(root.toJSONString())
                .execute();
        if(response.getStatus() != 200){
            return R.fail(response.body());
        }else {
            try {
                JSONObject data = JSON.parseObject(response.body());
                JSONObject result = new JSONObject();
                result.put("took",data.getIntValue("took"));
                JSONArray hits = data.getJSONObject("hits").getJSONArray("hits");
                result.put("size",hits.size());
                result.put("hits",hits);
                return R.data(result);
            }catch (Exception e){
                return R.fail(response.body());
            }
        }


    }

}
